"""Activity viewer."""

from hcp.auth.decorators import permission_required
from hcp.jinja import render_to_response
from hcp import models

@permission_required('view_activity')
def index(request):
    mods = models.Modification.objects.all().order_by('-timestamp')
    data = []
    for mod in mods:
        data.append((mod, _all_objs(mod)))

    return render_to_response('activity/index.html',
        {'data': data,
        },
        request=request)

def _all_objs(mod):
    result = []
    all_obj = mod.modificationobject_set.all().order_by('object_type')
    for obj in all_obj:
        result.append((obj, _all_attrs(obj)))
    return result

def _all_attrs(obj):
    result = []
    all_attr = obj.modificationattribute_set.all().order_by('attribute_name')
    for attr in all_attr:
        old = attr.modificationvalueold_set.all()
        new = attr.modificationvaluenew_set.all()
        result.append((attr, old, new))
    return result
